<?php
global $_W, $_GPC;
$weid = $this->_weid;
$from_user = $this->_fromuser;
$setting = $this->getSetting();
$cur_nave = 'marketingorder';
$status = 0;
$storeid = intval($_GPC['storeid']);
$title = '打款记录';

if (!empty($_GPC['status'])) {
    $status = intval($_GPC['status']);
}

$op = $_GPC['op'];

if ($op != 'shop') {
    //市场专员
    $marketing = pdo_fetch("SELECT * FROM " . tablename('weisrc_dish_marketing') . " WHERE weid = :weid AND from_user=:from_user LIMIT 1;", array(':weid' => $this->_weid, ':from_user' => $from_user));
    if (empty($marketing)) {
        message('您不是市场专员，无权访问！');
    }
}


$do = 'marketinglog';
if ($setting['auth_mode'] == 1 || empty($setting)) {
    $method = 'order'; //method
    $host = $this->getOAuthHost();
    $authurl = $host . 'app/' . $this->createMobileUrl($method, array('op' => $op, 'storeid' => $storeid), true) . '&authkey=1';
    $url = $host . 'app/' . $this->createMobileUrl($method, array('op' => $op, 'storeid' => $storeid), true);
    if (isset($_COOKIE[$this->_auth2_openid])) {
        $from_user = $_COOKIE[$this->_auth2_openid];
        $nickname = $_COOKIE[$this->_auth2_nickname];
        $headimgurl = $_COOKIE[$this->_auth2_headimgurl];
    } else {
        if (isset($_GPC['code'])) {
            $userinfo = $this->oauth2($authurl);
            if (!empty($userinfo)) {
                $from_user = $userinfo["openid"];
                $nickname = $userinfo["nickname"];
                $headimgurl = $userinfo["headimgurl"];
            } else {
                message('授权失败!');
            }
        } else {
            if (!empty($this->_appsecret)) {
                $this->getCode($url);
            }
        }
    }
} else {
    load()->model('mc');
    if (empty($_W['fans']['nickname'])) {
        mc_oauth_userinfo();
    }
    $from_user = $_W['fans']['openid'];
    $nickname = $_W['fans']['nickname'];
    $headimgurl = $_W['fans']['tag']['avatar'];
}

if (empty($from_user)) {
    message('会话已过期，请重新发送关键字!');
}


if ($op != 'shop') {
    $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketinglog') . " WHERE weid = :weid AND marketingid = :marketingid ORDER BY
 id DESC LIMIT 300;", array(':weid' => $weid, ':marketingid' => $marketing['id']));
} else {
    $list = pdo_fetchall("SELECT * FROM " . tablename('weisrc_dish_marketinghandlelog') . " WHERE weid = :weid AND type=1 and  logid = :logid ORDER BY
 id DESC LIMIT 300;", array(':weid' => $weid, ':logid' => $storeid));
    $show_fee_rate = intval($setting['fee_rate']);
    foreach ($list as $key => $value) {
        //日交易总金额
        $totalprice = pdo_fetchcolumn("SELECT sum(totalprice) FROM " . tablename($this->table_order) . " WHERE weid=:weid AND storeid=:storeid AND status=3
AND (paytype=1 OR paytype=2 OR paytype=4)
 AND ispay=1 AND ismerge=0 and  date_format(FROM_UNIXTIME(dateline), '%Y-%m-%d') = :date ", array(':weid' => $this->_weid, ':storeid' => $storeid, ':date' => $value['date']));

        $fee = floatval($totalprice)*floatval($show_fee_rate)/100;
        $totalprice = $totalprice-$fee;
        $totalprice = sprintf('%.3f', $totalprice);
        $list[$key]['totalprice'] = $totalprice;
    }
}

include $this->template($this->cur_tpl . '/marketinglog');